home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cjdates.exe / DATES.PAS < prev    next >
Pascal/Delphi Source File  |  1991-07-07  |  5KB  |  106 lines

  1.                 unit dates;
  2.  
  3. {This unit provides date calculation support for Turbo Pascal programs.  It
  4.  uses the universal date calculation routines in "dates.obj" which have been
  5.  designed to work with both Pascal and C, so programs written in either
  6.  language will have compatible Day Numbers.
  7.  
  8.  The heart of the scheme is the use of a Day Number which is related to
  9.  the "Julian Day Number", the number of days since a date in the distant
  10.  past.    Note that this is NOT the Julian DATE, which contins a day of the
  11.  YEAR.    The Day Number used by these routines has two useful properties:
  12.  
  13.  1.) The difference between two Day Numbers is the number of days between
  14.      those dates.
  15.  2.) The remainder from dividing the Day Number by 7 (DayNumber mod 7) gives
  16.      the day of the week, with 0 being Sunday thru 6 being Saturday.
  17.  
  18.  In addition, the conversions will not produce invalid results (unless you
  19.  get out of range, in which case zeroes are returned, so you can tell).
  20.  That is, any garbage Gregorian date (Month/Day/Year form) will give some
  21.  kind of Day Number, and all Day Numbers convert to valid Gregorian dates.
  22.  Thus you can check the validity of a Gregorian date by converting it to a
  23.  Day Number and back, and then comparing the Gregorian date you get back to
  24.  the original.    If they are different, the original date was invalid.
  25.  For example, if you convert 2/29/1900 to a Day Number you get 694082.
  26.  Convert it back and you get 3/1/1900, so 2/29/1900 is not a valid date.
  27.  And that's true.  Even though it looks like it should be a leap year, the
  28.  400-year Gregorian rule says that the leap year is dropped on the century
  29.  year unless that century year can be divided by 400 without a remainder.
  30.  So if we convert 2/29/2000 to a Day Number we get 730606 which converts back
  31.  to 2/29/2000, so that IS a valid date.
  32.  
  33.  Note that the Day Number tends to be what an invalid date "should have been".
  34.  That is, in the Feb 29, 1900 case shown above, it gave the Day Number of
  35.  March 1, 1900.  We can take advantage of this to find the Julian day of
  36.  the year.  It is simpky the difference between the Day Number of the desired
  37.  date and the Day Number of January ZERO (so Jan 1 will come out day 1) of
  38.  the same year.
  39.  
  40.  To convert a Julian date to a Gregorian date, Convert to a Day Number the
  41.  Month 1, the Day the Julian Day of the Year, and the given year, then
  42.  convert back to a Gregorian date.  For example, if we have the Julian date
  43.  335/1900, we converet 1/335/1900 to a Day Number (694357) which converts
  44.  back to 12/1/1900.  335/2000 converts (from 1/335/2000) to 730881, which
  45.  converts back to 11/30/2000.
  46.  
  47.  Please note that all values are UNSIGNED!  The Day Number will never be so
  48.  big as to appear as a negative value.  A negative value supplied as a Day
  49.  Number by the user will appear to ZDate as a giant (and out of range)
  50.  positive number.  Year, Month and Day are all WORDs, and are, thus, unsigned.
  51.  The caller to ZDate must supply words to hold the returned Gregorian date
  52.  components.  (You will note that they are VAR parameters.)
  53.  
  54.  When you call ZDay, the Year must not exceed 25599.  Dates earlier than
  55.  2/1/0 will not convert.  The Month and Day may have values in the range
  56.  0-65535, but using large values like this will reduce the maximum value
  57.  that you can supply for the Year.  Normally this will not be a problem.
  58.  For dates that cannot be converted, ZDay returns a ZERO value for the Day
  59.  Number.
  60.  
  61.  For years before Gregorian dates were used you will not get the correct
  62.  values,  The conversion to Gregorian dates began in the 14th century, but
  63.  were accepted at different times in different countries.  No checks are
  64.  made for dates before the Gregorian calendar was adopted.
  65.  
  66.  When you call ZDate, the Day Number you supply must be greater than 121
  67.  and less than 23920640.  If you supply a value outside this range, ZDate
  68.  will return a boolean FALSE and your three words (Year, Month and Day)
  69.  will be set to zero.
  70.  
  71.  NOTE: In all these routines, the Year is the FULL year, not just the last two
  72.        digits!!!!
  73.  
  74.  See also the companion pieces in "dates.inc", which you can either include
  75.  as is or read in with ^KR and edit to suit you own needs.}
  76.                         {(c)Copyright 1991 Crazy Jack}
  77.                              {All Rights Reserved}
  78. {$D-,O+}
  79.  
  80. {--------------------------------}INTERFACE{---------------------------------}
  81.  
  82. function ZDay(                   {Convert Gregorian date to Day Number.         }
  83.           Year,            {User-supplied year of Gregorian date}
  84.           Month,            {User-supplied Gregorian month.      }
  85.           Day : word        {User supplied day of month.         }
  86.         ) : longint;        {Function returns Day Number.         }
  87.  
  88. function ZDate(                 {Convert Day Number to Gregorian date:       }
  89.            DayNumber : longint;    {User-supplied Day Number.         }
  90.            var Year,        {User's word receives year.         }
  91.            Month,        {User's holder receives month.         }
  92.            Day : word        {User's holder receives day of month.}
  93.              ) : boolean;    {Function returns FALSE if Day Number|
  94.                     {is out of range, TRUE otherwise.    }
  95.  
  96. {------------------------------}IMPLEMENTATION{------------------------------}
  97.  
  98. function ZDay(Year, Month, Day : word) : longint; external;
  99.  
  100. function ZDate(DayNumber : longint; var Year, Month, Day : word ) : boolean;
  101.                                      external;
  102.  
  103. {$L dates}            {See "dates.asm" for details.}
  104.  
  105. end.
  106.